﻿using System;
using System.Collections.Generic;
using IOCDemo.Domain;
using IOCDemo.Interfaces;

namespace IOCDemo.Services
{
    public class MockAccountService : IAccountService
    {
        private readonly IAuditService auditService;
        private readonly ICacheService cacheService;
        private readonly ILoggingService loggingService;

        public MockAccountService(
            ILoggingService loggingService,
            ICacheService cacheService,
            IAuditService auditService)
        {
            this.loggingService = loggingService;
            this.cacheService = cacheService;
            this.auditService = auditService;
        }

        #region Implementation of IAccountService

        public IList<Account> GetAccounts(AuthToken authToken)
        {
            loggingService.Info("Getting accounts.");

            string cacheKey = string.Format("{0}_AccountList", authToken.SessionKey);

            if (!cacheService.Contains(cacheKey))
                cacheService.Put(cacheKey, new List<Account>
                                               {
                                                   new Account
                                                       {
                                                           AccountNumber = Guid.NewGuid().ToString(),
                                                           AccountType = "Checking",
                                                           Name = "Business Checking"
                                                       },
                                                   new Account
                                                       {
                                                           AccountNumber = Guid.NewGuid().ToString(),
                                                           AccountType = "Savings",
                                                           Name = "High Interest Savings"
                                                       }
                                               });

            return cacheService.Get(cacheKey) as IList<Account>;
        }

        public IList<Transaction> GetTransactions(AuthToken authToken,
                                                  TimeSpan searchWindow, int page, int pageSize)
        {
            loggingService.Info("Getting transactions.");

            string cacheKey = string.Format("{0}_TransactionList", authToken.SessionKey);

            if (!cacheService.Contains(cacheKey))
                cacheService.Put(cacheKey, new List<Transaction>
                                               {
                                                   new Transaction
                                                       {
                                                           Amount = Decimal.Parse("564.45"),
                                                           Date = DateTime.UtcNow,
                                                           FromAccount = GetAccounts(authToken)[0],
                                                           ToAccount = GetAccounts(authToken)[1]
                                                       }
                                               });

            return cacheService.Get(cacheKey) as IList<Transaction>;
        }

        public void Transfer(AuthToken authToken, Transaction transaction)
        {
            loggingService.Debug("Processing transfer.");
            auditService.RecordTransaction(transaction);
        }

        #endregion
    }
}